﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////

#pragma once
#include "NdbCustomUpgraderPropertyAccessor.h"

namespace gcmp
{
    //////////////////////////////////////////////////////////////////////////
    // 自定义升级程序里使用下面两个类方便存取属性
    class NdbCustomUpgraderOldObject
    {
    public:
        NdbCustomUpgraderOldObject(const NdbObjectMemoryModelNode *pObjectMemoryModelNode) : m_pObjectMemoryModelNode(pObjectMemoryModelNode) {}

        const NdbObjectMemoryModelNode * GetOldMemoryModelNode() const { return m_pObjectMemoryModelNode; }

        // T是属性的类型
        template<typename T>
        bool HasProperty(const std::wstring& propertyName) const
        {
            NdbCustomUpgraderPropertyGetter<T> propertyGetter(m_pObjectMemoryModelNode);
            return propertyGetter.HasProperty(propertyName);
        }

        // T是属性的类型
        template<typename T>
        bool GetPropertyValue(const std::wstring& propertyName, T &value) const
        {
            NdbCustomUpgraderPropertyGetter<T> propertyGetter(m_pObjectMemoryModelNode);
            return propertyGetter.GetPropertyValue(propertyName, value);
        }

        template<typename K, typename V>
        bool GetMapPropertyValue(const std::wstring& propertyName, gcmp::NdbMap<K, V> &value) const
        {
            NdbCustomUpgraderMapPropertyGetter<K, V> propertyGetter(m_pObjectMemoryModelNode);
            return propertyGetter.GetPropertyValue(propertyName, value);
        }

        template<typename K, typename V>
        bool GetUnorderedMapPropertyValue(const std::wstring& propertyName, gcmp::NdbUnorderedMap<K, V> &value) const
        {
            NdbCustomUpgraderUnorderedMapPropertyGetter<K, V> propertyGetter(m_pObjectMemoryModelNode);
            return propertyGetter.GetPropertyValue(propertyName, value);
        }

    private:
        const NdbObjectMemoryModelNode * m_pObjectMemoryModelNode;
    };

    class NdbCustomUpgraderNewObject
    {
    public:
        NdbCustomUpgraderNewObject(NdbObjectMemoryModelNode *pObjectMemoryModelNode) : m_pObjectMemoryModelNode(pObjectMemoryModelNode) {}

        NdbObjectMemoryModelNode * GetNewMemoryModelNodeFW() { return m_pObjectMemoryModelNode; }

        // T是属性的类型
        template<typename T>
        bool SetPropertyValue(const std::wstring& propertyName, const T& value)
        {
            NdbCustomUpgraderPropertySetter<T> propertySetter(m_pObjectMemoryModelNode);
            return propertySetter.SetPropertyValue(propertyName, value);
        }

    private:
        NdbObjectMemoryModelNode * m_pObjectMemoryModelNode;
    };
}
